using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

using Iservice;
using Demo.Net.Model;

using Utils;

namespace Demo.Net.BlogAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]

    public class CarController : ControllerBase
    {
        private readonly IBaseService<ParkingSpace> _parking;

        public CarController(IBaseService<ParkingSpace> parking)
        {
            this._parking = parking;
        }
        //获取车位信息表的所有数据
       //获取车位信息表的所有数据
        [HttpGet]
        public async Task<ActionResult<ApiReturn>> FindAll()
        {
            var allParking = await _parking.FindAll();
            if (allParking.Count == 0)
            {
                return ApiRetrunsHelper.Failed("没有车位数据");
            }

            return ApiRetrunsHelper.Success(allParking);

        }
        // 根据条件查询车位信息
        [HttpGet]
        public async Task<ActionResult<ApiReturn>> FindAllByCondition(string searchTerm)
        {
            var res = await _parking.FindAllByCondition(
                a => a.Id.ToString() == searchTerm
                || a.Code.Contains(searchTerm)
                || a.Type.Contains(searchTerm)
                || a.Status.Contains(searchTerm)
                || a.RentStartTime.ToString().Contains(searchTerm)
                || a.RentEndTime.ToString().Contains(searchTerm)
                );
            if (res.Count == 0)
            {
                return ApiRetrunsHelper.Failed("没有匹配的数据");
            }

            return ApiRetrunsHelper.Success(res);
        }
        //根据id查找
        [HttpGet]
        public async Task<ActionResult<ApiReturn>> Find(int id)
        {
            var res = await _parking.Find(s => s.Id == id);
            if (res.Count == 0)
            {
                return ApiRetrunsHelper.Failed("没有该数据");
            }
            return ApiRetrunsHelper.Success(res);
        }
        //添加
        [HttpPost]
        public async Task<ActionResult<ApiReturn>> Add(ParkingSpace parkings)
        {
            var res = await _parking.Add(parkings);
            if (res == false)
            {
                return ApiRetrunsHelper.Failed("没有数据");
            }
            return ApiRetrunsHelper.Success(res);
        }
        //编辑
        [HttpPut]
        public async Task<ActionResult<ApiReturn>> Update(ParkingSpace parkings)
        {

            var res = await _parking.UpdateAsync(parkings);
            if (res == false)
            {
                return ApiRetrunsHelper.Failed("没有数据");
            }
            return ApiRetrunsHelper.Success(res);
        }
        //删除
        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiReturn>> Delect(int id)
        {
            var result = await _parking.Delete(id);
            if (result)
            {
                return ApiRetrunsHelper.Success(result);
            }
            else
            {
                return ApiRetrunsHelper.Failed("没有数据");
            }

        }
    }
}